<?php
class Replies extends Public_Controller
{
	public function __construct()
	{
		parent::__construct();
	}
	
	public function form($topic_id,$id = NULL)
	{
		$data['topic'] = new Post($topic_id);
		$data['edit'] = (is_login() && ( user()->level_id < 3 OR user()->post->get_by_id($id)->id )) ? new Post($id) : new Post;
		//media
		$this->template->append_metadata('<script type="text/javascript" src="media/tiny_mce/tiny_mce.js"></script>');
		$this->template->append_metadata('<script type="text/javascript" src="media/js/tinymce.js"></script>');
		$this->template->build('replies/form',$data);
	}
	
	public function save($topic_id,$id = NULL)
	{
		if($_POST) 
		{
			$reply = (is_login() && ( user()->level_id < 3 OR user()->post->get_by_id($id)->id )) ? new Post($id) : new Post;
			$reply->from_array($_POST);
			$reply->topic_id = $topic_id;
			if($reply->id) $reply->modified = date('Y-m-d H:i:s');
			if(is_login() && !$id) $reply->user_id = user()->id;
			if($_POST['delete_image']==1)
			{
				$reply->delete_file('uploads/webboard/','image');
				$reply->image = NULL;
			} 
			if($_FILES['image']['name'])
			{
				$reply->delete_file('uploads/webboard/','image');
				$reply->image = $reply->upload($_FILES['image'],'uploads/webboard/',600,600,'limit_x');
			}
			$reply->save();
			set_notify('success', lang('save_data_complete'));
		}
		redirect('webboard/topics/view/'.$topic_id);
	}
	
	public function delete($topic_id,$id)
	{
		if(is_login() && ( user()->level_id < 3 OR user()->post->get_by_id($id)->id  ))
		{
			$reply = (is_login() && ( user()->level_id < 3 OR user()->post->get_by_id($id)->id )) ? new Post($id) : new Post;
			$reply->delete_file('uploads/webboard/','image');
			$reply->delete();
			set_notify('success', lang('delete_data_complete'));
		}
		redirect('webboard/topics/view/'.$topic_id);
	}	
}
?>